#include "TextureManager.hpp"
#include <cassert>
#include <stdexcept>

TextureManager::TextureManager():
m_res()
{
}
TextureManager::~TextureManager()
{
}

TextureManager& TextureManager::instance()
{
    static TextureManager tm;
    return tm;
}

TexturePtr TextureManager::resource(const std::string& fileName)
{
    if(m_res.find(fileName) != m_res.end())
        return m_res[fileName];
        
    std::string path = "media/images/";
    path += fileName;
        
    loadResource(path);   
    assert(m_res.find(fileName) != m_res.end());
    
    return m_res[fileName];
}

void TextureManager::loadResource(const std::string &path)
{
    sf::Texture *i = new sf::Texture();   
    if(i->loadFromFile(path))
        m_res[path.substr(13)] = TexturePtr(i);
    
    else
    {
        delete i;
        throw std::runtime_error("Unable to load texture from path : " + path);
    }
}

